/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yournamehere.server;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author D4v1D
 */
public class ScoreController {
    private final String fileName="SudokuResult.txt";
    private FileReader r;
    private FileWriter f;
    public ScoreController(){
        
        //f = new FileWriter()
    }
    public Score[] getScore(String level){
        ArrayList<String> allScore = new ArrayList<String>();
        ArrayList<Score> allLevelScore = new ArrayList<Score>();

        try {
            r = new FileReader(fileName);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ScoreController.class.getName()).log(Level.SEVERE, null, ex);
        }
        BufferedReader b = new BufferedReader(r);
        String temp;
        try {
            while ((temp = b.readLine()) != null) {
                allScore.add(temp);
            }
            b.close();
            r.close();
        } catch (IOException ex) {
            Logger.getLogger(ScoreController.class.getName()).log(Level.SEVERE, null, ex);
        }
        for(String line:allScore){
            String[] tab = line.split("|");
            Score score=new Score(tab[0],tab[1],tab[2]);
            if(score.getLevel().equals(level)){
                allLevelScore.add(score);
            }
        }
        return sortScore(allLevelScore);
    }
    public Score[] sortScore(ArrayList<Score> tab){
        Score[] winners = new Score[10];
        for(int i=0;i<tab.size()-1;++i){
            int j=i;
            while(j>0 && tab.get(j).getTime()>tab.get(j+1).getTime()){
                Score temp = tab.get(i+1);
                tab.remove(i+1);
                tab.add(i,temp);
                j--;
            }
        }
        for(int i=0;i<tab.size() && i<10;++i){
            winners[i]=tab.get(i);
        }
        return winners;
    }
    public boolean saveScore(String newInput){
        try {
            r = new FileReader(fileName);
            BufferedReader b = new BufferedReader(r);
            String temp,total=newInput;
            try {
                while ((temp = b.readLine()) != null) {
                    total=total + "\n" + temp ;
                }
                b.close();
                r.close();
            } catch (IOException ex) {
                Logger.getLogger(ScoreController.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
            try {
                f = new FileWriter(fileName);
                f.write(total);
                f.flush();
                f.close();
            } catch (IOException ex) {
                Logger.getLogger(ScoreController.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(ScoreController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
}
